package workforce.limousine.struts;

import limousine.util.SelectItem;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import limousine.vo.Users;
import limousine.vo.Role;
import limousine.dbservice.RoleService;
import limousine.util.Service;
import limousine.util.Token;
import limousine.util.Func;
import java.util.List;

public class RoleEditInit extends Action {

    private String errorMsg = "Process Fail";
    private Func func = new Func();

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        HttpSession session = request.getSession();
        System.out.println("***RoleEditInit");

        Users user = (Users) session.getAttribute(Token.USER_SESSION);
        if (user == null) {
            System.out.println("--SESSION EXPIRE--");
            return mapping.findForward(Token.SESSION_EXPIRE);
        }
       /* if (!user.getRole_name().equals("Admin")) {
            System.out.println("--Not Admin Hack--");
            return mapping.findForward(Token.SESSION_EXPIRE);
        }*/

        //        DataSource ds = this.getDataSource(request, Token.DATASOURCE_NAME);
        Service service = new Service();
        service.setServletContext(this.getServlet().getServletContext());
        DataSource ds = service.getDataSource();
        service.removeMsgAttribute(request, session, "");

        session.removeAttribute(Token.ROLE);
        session.removeAttribute(Token.ROLE_ENABLE_LIST);
        DynaActionForm daf = (DynaActionForm) form;
        
        String id = request.getParameter("eid");
        
        RoleService os = new RoleService();
        os.setDataSource(ds);
        Role o = os.getById(id);
        if( o != null){
            daf.set("id",o.getId());
            daf.set("name", o.getName());
            daf.set("detail",o.getDetail());
            List<SelectItem> enableList = service.getEnableList(o.getEnable());
            session.setAttribute(Token.ROLE_ENABLE_LIST,enableList);
        }

        
        return mapping.findForward(Token.SUCCESS);
        
    }

}
